﻿using System;
using System.Xml;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.Reflection;
using System.Text;
//using OrganizationName.Factory;
//using OrganizationName.Xml;
using System.Collections;
using System.Collections.Generic;
using OrganizationName.Factory;
using OrganizationName.Runtime;
using System.CodeDom;
using System.CodeDom.Compiler;

namespace OrganizationName.Lab
{
    public class Program
    {
        static DynamicFactory dynamicFactoryInstance;

        static void EDRA()
        {
            dynamicFactoryInstance = new DynamicFactory();
            Console.WriteLine("ResponseMessage: {0}", RuntimeContext.Current["ResponseMessage"]);
            bool isDisposable = dynamicFactoryInstance.Exists("InternetBankingCorporateConfirm", typeof(string));

            object p1 = dynamicFactoryInstance.Get("InternetBankingCorporateConfirm");
            Console.WriteLine("ResponseMessage: {0}", RuntimeContext.Current["ResponseMessage"]);
            Console.WriteLine("p1: {0}", p1);
            Console.WriteLine("ResponseMessage == p1: {0}", object.ReferenceEquals(p1, RuntimeContext.Current["ResponseMessage"]));

            // dynamicFactoryInstance.Dispose();

            Console.WriteLine("Waiting..");
            Console.ReadLine();

            object p2 = dynamicFactoryInstance.Get("InternetBankingFetch");

            Console.WriteLine("Waiting..");
            Console.ReadLine();
        }

        static void Main(string[] args)
        {
            Stopwatch sw = Stopwatch.StartNew();

            sw.Reset();
            sw.Start();
            LoaderOutput loaderOutput = Loader.Instance.Load("Akbank.InternetBanking.Common.MessageDefinitions");
            Assembly[] referencedAssemblies = loaderOutput.References.ToArray();
            Console.WriteLine("Assembly load ended in {0} ms", sw.Elapsed.TotalMilliseconds);

            sw.Reset();
            sw.Start();
           // System.Type type = Loader.Instance.LoadType("Akbank.InternetBanking.WebApplication.UIProcess.HttpHandlers.GenericAjaxDataHandler, Akbank.InternetBanking.WebApplication.UIProcess").Type;
            Console.WriteLine("Type load ended in {0} ms", sw.Elapsed.TotalMilliseconds);

            // Activator.CreateInstance(type);

            string code = "class testClass { public testClass() { throw new System.Exception(\"ggg\"); } }";
            List<string> assemblies = new List<string>();
            assemblies.Add(new Uri(loaderOutput.Assembly.CodeBase).LocalPath);
            foreach (Assembly a in referencedAssemblies)
            {
                assemblies.Add(new Uri(a.CodeBase).LocalPath);
            }
            CodeDomProvider prov = CodeDomProvider.CreateProvider("C#");
            CompilerResults results = prov.CompileAssemblyFromSource(new CompilerParameters(assemblies.ToArray()), new string[] { code });

            Console.WriteLine("# Assemblies: {0}", AppDomain.CurrentDomain.GetAssemblies().Length);

            // Activator.CreateInstance(results.CompiledAssembly.GetTypes()[0]);

            //OrganizationName.DynamicData.Data settingsData_WEB = OrganizationName.DynamicData.Data.FromXml("Settings.Web", "Settings", @"C:\Projects\Akbank.InternetBanking.2010\BisKis\Source\Presentation\WebApplication.UI\staticdata\settings.xml");

            //OrganizationName.DynamicData.Schema settingsSchema = OrganizationName.DynamicData.Schema.FromDatabase("Settings", @"data source=SQLTSRV4\SQLTAKB5,1453;initial catalog=TSBX00;Uid=tsbxusr1;Pwd=T5bx11;");
            //OrganizationName.DynamicData.Data settingsData_APP = OrganizationName.DynamicData.Data.FromXml("Settings.App", settingsSchema, @"C:\Projects\Akbank.InternetBanking.2010\BisKis\Source\Business\EDRAWebService\staticdata\settings.xml");

            //string insertScript_WEB = settingsData_WEB.GetInsertScript();
            //string insertScript_APP = settingsData_APP.GetInsertScript();

            dynamicFactoryInstance = new DynamicFactory();
            dynamicFactoryInstance.Configure("runtime.xml");

            //object presenter = dynamicFactoryInstance.Get("SettingsPresenter");

            Thread[] threads = new Thread[5];

            for (int i = 0; i < 5; i++)
            {
                threads[i] = new Thread(new ThreadStart(Worker));
                threads[i].Start();
            }

            Console.WriteLine("Waiting..");
            Console.ReadLine();

            for (int i = 0; i < 5; i++)
            {
                threads[i].Abort();
            }
        }

        static Random randomInstance = new Random();
        static void Worker()
        {
            int i = -1;
            while (i++ < 100)
            {
                Thread.Sleep(randomInstance.Next(200, 500));
                OrganizationName.Xml.XmlExtendedDocument o2 = dynamicFactoryInstance.Get<System.Xml.XmlDocument>() as OrganizationName.Xml.XmlExtendedDocument;
                // OrganizationName.Xml.XmlDocument o2 = new _1878166724.DynamicFactoryImpl().Create<System.Xml.XmlDocument>() as OrganizationName.Xml.XmlDocument;
                Console.WriteLine("THREAD: {0}, HASH: {1}, EXTENSIONS HASH: {2}", Thread.CurrentThread.ManagedThreadId, o2.GetHashCode(), o2.Extensions != null ? o2.Extensions.GetHashCode().ToString() : "NULL");
                o2 = null;
            }
        }
    }
}
